<template>
  <el-aside :width="width">
    <el-menu
      style="--el-menu-bg-color: #545c64; --el-menu-text-color: #fff"
      :collapse="isCollapse"
      :collapse-transition="false"
    >
      <h3 v-show="!isCollapse">后台管理系统</h3>
      <h3 v-show="isCollapse">后台</h3>
      <el-menu-item
        v-for="item in nochildren"
        :index="item.path"
        :key="item.path"
      >
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>

      <el-sub-menu
        v-for="item in haschildren"
        :index="item.path"
        :key="item.path"
      >
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
            v-for="(subItem, subIndex) in item.children"
            :index="subItem.path"
            :key="subItem.path"
          >
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { useAllDataStore } from '@/stores'
import { computed, ref } from 'vue'

const list = ref([
  {
    path: '/home',
    name: 'home',
    label: '首页',
    icon: 'house',
    ur1: 'Home',
  },
  {
    path: '/mall',
    name: 'mall',
    label: '商品管理',
    icon: 'video-play',
    url: 'Mall',
  },
  {
    path: '/user',
    name: 'user',
    label: '用户管理',
    icon: 'user',
    url: 'User',
  },
  {
    path: 'other',
    label: '其他',
    icon: 'location',
    children: [
      {
        path: '/pagel',
        name: 'pagel',
        label: '页面1',
        icon: 'setting',
        url: 'Pagel',
      },
      {
        path: '/page2',
        name: 'page2',
        label: '页面2',
        icon: 'setting',
        url: 'Page2',
      },
    ],
  },
])

// 菜单数据
const nochildren = computed(() => list.value.filter((item) => !item.children))
const haschildren = computed(() => list.value.filter((item) => item.children))

const store = useAllDataStore()
// 菜单收缩
const isCollapse = computed(() => store.state.isCollapse)
const width = computed(() => (store.state.isCollapse ? '64px' : '180px'))
</script>

<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.el-menu {
  border-right: none;
  h3 {
    text-align: center;
    line-height: 48px;
    color: #fff;
  }
}
.el-aside {
  height: 100%;
  background-color: #545c64;
}
</style>
